package com.tiny.persistence.repository.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

import javax.persistence.Query;

import org.apache.log4j.Logger;

import com.ai.web.cache.Constants;
import com.ai.web.cache.UserContext;
import com.ai.web.vo.QueryData;
import com.tiny.persistence.domain.Repair;
import com.tiny.persistence.repository.plus.RepairRepoPlus;
/**
 * 报修仓储层
* @ClassName: RepairRepoImpl 
* @Description: TODO 
* @author:  
* @date: 2014年3月23日
*
 */
public class RepairRepoImpl extends BaseRepoImpl<Repair> implements RepairRepoPlus{

    private static Logger logger = Logger.getLogger(RepairRepoImpl.class);

    @Override
    public List<Repair> getListByCondition(Map<String, Object> condition) {
        StringBuffer sqlquery = new StringBuffer();
        sqlquery.append("from Repair a where 1=1 ");

        //查询条件
        String status = null;
        if(condition.get("status")!=null){
        	status = String.valueOf(condition.get("status"));
        }
        if(status!=null){
            sqlquery.append(" and a.status=:status"); 
        }
        String serialNumber = null;
        if(condition.get("serialNumber")!=null){
            serialNumber = String.valueOf(condition.get("serialNumber"));
        }
        
        if(serialNumber!=null){
            sqlquery.append(" and a.serialNumber like :serialNumber"); 
        }
        String orgName = null;
        if(condition.get("orgName")!=null){
        	orgName = String.valueOf(condition.get("orgName"));
        }
        
        if(orgName!=null){
            sqlquery.append(" and a.orgName like :orgName "); 
        }
        sqlquery.append(" order by a.repairDate desc ");
        Query query = em.createQuery(sqlquery.toString());
        if(status!=null){
            query.setParameter("status", Byte.valueOf(status));
        }
        if(serialNumber!=null){
            query.setParameter("serialNumber", "%"+serialNumber+"%");
        }
        if(orgName!=null){
            query.setParameter("orgName", "%"+orgName+"%");
        }
        if(condition.get(Constants.KEY_RECORD_FROM)!=null && condition.get(Constants.USER_CONTEXT)!=null){
        	query.setFirstResult((Integer) condition.get(Constants.KEY_RECORD_FROM));
        	UserContext uc = (UserContext) condition.get(Constants.USER_CONTEXT);
        	if(uc!=null){
                query.setMaxResults(uc.getPageSize());
        	}
        }        
        @SuppressWarnings("unchecked")
		List<Repair> list = query.getResultList();
        
        logger.info("======= " + list);
        
        return list;
    }

    @Override
    public int getCountByCondition(Map<String, Object> condition) {
        
        StringBuffer sqlquery = new StringBuffer();
        sqlquery.append("select count(*) from Repair a where 1=1 ");
        
        //查询条件
        //查询条件
        String status = null;
        if(condition.get("status")!=null){
        	status = String.valueOf(condition.get("status"));
        }
        if(status!=null){
            sqlquery.append(" and a.status=:status"); 
        }
        String serialNumber = null;
        if(condition.get("serialNumber")!=null){
            serialNumber = String.valueOf(condition.get("serialNumber"));
        }
        if(serialNumber!=null){
            sqlquery.append(" and a.serialNumber like :serialNumber"); 
        }
        
        String orgName = null;
        if(condition.get("orgName")!=null){
        	orgName = String.valueOf(condition.get("orgName"));
        }
        
        if(orgName!=null){
            sqlquery.append(" and a.orgName like :orgName "); 
        }
        Query query = em.createQuery(sqlquery.toString());
        if(status!=null){
            query.setParameter("status", Byte.valueOf(status));
        }
        if(serialNumber!=null){
            query.setParameter("serialNumber", "%"+serialNumber+"%");
        }
        if(orgName!=null){
            query.setParameter("orgName", "%"+orgName+"%");
        }
        Object obj = query.getSingleResult();
        
        logger.info("======= count=" + obj);
        
        return Integer.parseInt(obj.toString());
    }

	@SuppressWarnings("unchecked")
	public List<Repair> getListByOpenId(String openId) {
		Query query = em.createQuery("from Repair as re where re.bankUserPhone in(select mobilePhone from WxUser where openId=:openId) order by repairDate desc");
		query.setParameter("openId", openId);
		return query.getResultList();
	}

	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> getCountByStatus() {
		Query query = em.createQuery("select count(status) as count, status as status from Repair group by status");
		return query.getResultList();
	}
	
	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> getCountByStatus(String repairDateStr) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");	
		String startTime = repairDateStr+" 00:00:00";
		String endTime = repairDateStr+" 23:59:59";
		Query query = em.createQuery("select count(status) as count, status as status from Repair where repairDate >=:startTime and repairDate <=:endTime group by status");
		try {
			query.setParameter("startTime", sdf.parse(startTime));
			query.setParameter("endTime", sdf.parse(endTime));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return query.getResultList();
	}

	public QueryData queryListAndCount(int status,
			Map<String, Object> conditionMap) {
		// TODO Auto-generated method stub
		return null;
	}

}
